const errorType = require("../constans/error-types");
const { logger } = require("../../logger");
const { Op } = require("sequelize");
const { authParams, authParamsBody } = require("../utils/auth-params");
const Apply = require("../model/apply.model");
const User = require("../model/user.model");

class ApplyController {
  async create(ctx, next) {
    // authParamsBody(ctx, ctx.request.body);
    await Apply.create(ctx.request.body)
      .then((res) => {
        ctx.success(res);
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  async detail(ctx, next) {
    const { applyId } = ctx.params;
    if (!applyId) {
      const error = new Error(errorType.NOPARAMETER);
      return ctx.app.emit("error", error, ctx);
    }
    await Apply.findOne({
      where: { id: applyId },
      raw: true,
    })
      .then((res) => {
        ctx.success(res);
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }

  async list(ctx, next) {
    const { limit, offset, keyword } = ctx.query;
    if (!limit || !offset) {
      const error = new Error(errorType.NOPARAMETER);
      return ctx.app.emit("error", error, ctx);
    }
    await Apply.findAndCountAll({
      where: {
        thing_name: {
          [Op.like]: `%${keyword}%`,
        },
      },
      include: {
        attributes: ["name", "id"],
        model: User,
      },
      limit: parseInt(limit),
      offset: parseInt(limit) * parseInt(offset - 1),
    })
      .then((res) => {
        ctx.success({
          list: res.rows,
          total: res.count,
          limit,
          offset,
        });
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  async update(ctx, next) {
    await Apply.update(ctx.request.body, {
      where: {
        id,
      },
    })
      .then((res) => {
        res[0] ? ctx.success() : ctx.fail();
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  async remove(ctx, next) {
    const { applyId } = ctx.params;
    await Apply.destroy({
      where: {
        id: applyId,
      },
    })
      .then((res) => {
        res ? ctx.success() : ctx.fail("");
      })
      .catch((err) => {
        ctx.fail();
        logger.error(err);
      });
  }

  // 修改审批状态
  async changeStatus(ctx, next) {
    const { status, id } = ctx.request.body;
    authParams(ctx, id);
    await Apply.update(
      { approval_status: status },
      {
        where: { id },
        raw: true,
      }
    )
      .then((res) => {
        ctx.success();
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }

  // 修改申请物品类型
  async changeTypes(ctx, next) {
    const { type, id } = ctx.request.body;
    await Apply.update(
      { types: type },
      {
        where: { id },
        raw: true,
      }
    )
      .then((res) => {
        ctx.success("修改成功");
      })
      .catch((err) => {
        ctx.fail(99);
        logger.error(err);
      });
  }
}

module.exports = new ApplyController();
